<template>
  <div>
     <Row>
        <form enctype="multipart/form-data">
              <input type="file" icon="ios-download-outline" @change="onFileChange">
              <Button type="ghost" icon="ios-cloud-upload-outline" @click="onImport" >导入Excel</Button>
        </form>
       
        <input v-model="gameId" placeholder="游戏id" ></input>
        <input v-model="machineType" placeholder="machineType" ></input>
        <input v-model="gameType" placeholder="gameType" ></input>
        <input v-model="betId" placeholder="betId" ></input>
        <Button type="primary" @click="onSearch">查询</Button>
        <Button type="primary" size="large" @click="onExport"  icon="ios-download-outline"></Icon>导出EXCEL</Button>
    </Row>
    <Table ref="table" class="bottom-table" :border="true" :stripe="true" :show-header="true" :data="tableData" :columns="tableColumns" ></Table>
    <Page class="bottom-page" :total="totalPage" :current="crtPage" :page-size="pageSize" show-total show-elevator @on-change="onChange"></Page>
    <Modal
        title="编辑changestatus"
        v-model="editChangeStatusModal" ok-text="提交" @on-ok="onSubmit">
      <Form  label-position="left" :model="formEdit" :label-width="100">
        <Form-item label="gameId">
            <Input v-model="formEdit.gameId" disabled ></Input>
        </Form-item>
        <Form-item label="machineType">
            <Input v-model="formEdit.machineType" disabled ></Input>
        </Form-item>
        <Form-item label="gameType">
            <Input v-model="formEdit.gameType" disabled></Input>
        </Form-item>
        <Form-item label="betId">
            <Input v-model="formEdit.betId" disabled></Input>
        </Form-item>
        <Form-item label="spitMinRate">
            <Input v-model="formEdit.spitMinRate"></Input>
        </Form-item>
        <Form-item label="spitMaxRate">
            <Input v-model="formEdit.spitMaxRate"></Input>
        </Form-item>
        <Form-item label="eatMinRate">
            <Input v-model="formEdit.eatMinRate"></Input>
        </Form-item>
        <Form-item label="eatMaxRate">
            <Input v-model="formEdit.eatMaxRate"></Input>
        </Form-item>
        <Form-item label="spitBackMinRate">
            <Input v-model="formEdit.spitBackMinRate"></Input>
        </Form-item>
        <Form-item label="spitBackMaxRate">
            <Input v-model="formEdit.spitBackMaxRate"></Input>
        </Form-item>
        <Form-item label="eatBackMinRate">
            <Input v-model="formEdit.eatBackMinRate"></Input>
        </Form-item>
        <Form-item label="eatBackMaxRate">
            <Input v-model="formEdit.eatBackMaxRate"></Input>
        </Form-item>
      </Form>
    </Modal>
  </div>
</template>
<script>

import {getChangeStatuses, getAllChangeStatuses, modifyChangeStatus, importChangeStatuses} from '@/api/game.js'
import {getVO} from '@/common/proto-vo.js'
import {exportExcel, importExcel} from '@/common/excelUtil.js'
export default {
  data () {
    return {
      tableData: [],
      gameId: '',
      machineType: '',
      gameType: '',
      betId: '',
      filename: 'change_status',
      crtPage: 1,
      totalPage: 0,
      pageSize: 10,
      editChangeStatusModal: false,
      results: '',
      formEdit: {
        gameId: '',
        machineType: '',
        gameType: '',
        betId: '',
        spitMinRate: '',
        spitMaxRate: '',
        eatMinRate: '',
        eatMaxRate: '',
        spitBackMinRate: '',
        spitBackMaxRate: '',
        eatBackMinRate: '',
        eatBackMaxRate: ''
      }
    }
  },
  created () {
    this.onSearch()
  },
  methods: {
    onSearch () {
      let tGameId = -1
      if (this.gameId !== '') {
        tGameId = parseInt(this.gameId)
      }

      let tMachineType = -1
      if (this.machineType !== '') {
        tMachineType = parseInt(this.machineType)
      }

      let tGameType = -1
      if (this.gameType !== '') {
        tGameType = parseInt(this.gameType)
      }

      let tBetId = -1
      if (this.betId !== '') {
        tBetId = parseInt(this.betId)
      }

      console.log('tGameId:', tGameId, ',tMachineType:', tMachineType, ',tGameType:', tGameType, ',tBetId:', tBetId)
      getChangeStatuses(tGameId, tMachineType, tGameType, tBetId, this.crtPage, this.pageSize).then((data) => {
        if (!data) {
          return
        }
        this.tableData = []
        let ChangeStatusVO = getVO('ChangeStatusVO')
        let pv = data.data.data
        let len = pv.data.length
        for (let i = 0; i < len; i++) {
          let vo = ChangeStatusVO.decode(pv.data[i])
          vo.spitMinRate = vo.spitMinRate.toFixed(5)
          vo.spitMaxRate = vo.spitMaxRate.toFixed(5)
          vo.eatMinRate = vo.eatMinRate.toFixed(5)
          vo.eatMaxRate = vo.eatMaxRate.toFixed(5)
          vo.spitBackMinRate = vo.spitBackMinRate.toFixed(5)
          vo.spitBackMaxRate = vo.spitBackMaxRate.toFixed(5)
          vo.eatBackMinRate = vo.eatBackMinRate.toFixed(5)
          vo.eatBackMaxRate = vo.eatBackMaxRate.toFixed(5)
          this.tableData.push(vo)
        }
        this.totalPage = pv.intValues[1]
        console.log(this.tableData)
      })
    },
    onChange (item) {
      console.log(item)
      this.crtPage = item
      this.onSearch()
    },
    onUploadSuccess (response, file, fileList) {
      this.$Message.info('load success!!!')
      console.log(response)
      console.log(file)
      console.log(fileList)
    },
    showEditChangeStatus (index) {
      let data = this.tableData[index]
      this.formEdit.gameId = data.gameId
      this.formEdit.machineType = data.machineType
      this.formEdit.gameType = data.gameType
      this.formEdit.betId = data.betId
      this.formEdit.spitMinRate = data.spitMinRate
      this.formEdit.spitMaxRate = data.spitMaxRate
      this.formEdit.eatMinRate = data.eatMinRate
      this.formEdit.eatMaxRate = data.eatMaxRate
      this.formEdit.spitBackMinRate = data.spitBackMinRate
      this.formEdit.spitBackMaxRate = data.spitBackMaxRate
      this.formEdit.eatBackMinRate = data.eatBackMinRate
      this.formEdit.eatBackMaxRate = data.eatBackMaxRate

      this.editChangeStatusModal = true
    },
    onSubmit () {
      if (this.formEdit.gameId === '' || this.formEdit.machineType === '' || this.formEdit.gameType === '' || this.formEdit.betId === '' || this.formEdit.spitMinRate === '' || this.formEdit.spitMaxRate === '' || this.formEdit.eatMinRate === '' || this.formEdit.eatMaxRate === '' || this.formEdit.spitBackMinRate === '' || this.formEdit.spitBackMaxRate === '' || this.formEdit.eatBackMinRate === '' || this.formEdit.eatBackMaxRate === '') {
        this.$Message.error('编辑参数不能为空!!!')
        return
      }

      let ChangeStatusVO = getVO('ChangeStatusVO')
      var changeStatusVO = new ChangeStatusVO()
      changeStatusVO.gameId = parseInt(this.formEdit.gameId)
      changeStatusVO.machineType = Number(this.formEdit.machineType)
      changeStatusVO.gameType = parseInt(this.formEdit.gameType)
      changeStatusVO.betId = parseInt(this.formEdit.betId)
      changeStatusVO.spitMinRate = Number(this.formEdit.spitMinRate)
      changeStatusVO.spitMaxRate = Number(this.formEdit.spitMaxRate)
      changeStatusVO.eatMinRate = Number(this.formEdit.eatMinRate)
      changeStatusVO.eatMaxRate = Number(this.formEdit.eatMaxRate)
      changeStatusVO.spitBackMinRate = Number(this.formEdit.spitBackMinRate)
      changeStatusVO.spitBackMaxRate = Number(this.formEdit.spitBackMaxRate)
      changeStatusVO.eatBackMinRate = Number(this.formEdit.eatBackMinRate)
      changeStatusVO.eatBackMaxRate = Number(this.formEdit.eatBackMaxRate)

      modifyChangeStatus(changeStatusVO).then((data) => {
        if (!data) {
          return
        }
        let success = data.data.data.intValues[0]
        if (success === 1) {
          let len = this.tableData.length
          for (let i = 0; i < len; i++) {
            let data = this.tableData[i]
            if (data.id === this.formEdit.id && data.gameId === this.formEdit.gameId) {
              this.tableData[i].spitMinRate = this.formEdit.spitMinRate
              this.tableData[i].spitMaxRate = this.formEdit.spitMaxRate
              this.tableData[i].eatMinRate = this.formEdit.eatMinRate
              this.tableData[i].eatMaxRate = this.formEdit.eatMaxRate
              this.tableData[i].spitBackMinRate = this.formEdit.spitBackMinRate
              this.tableData[i].spitBackMaxRate = this.formEdit.spitBackMaxRate
              this.tableData[i].eatBackMinRate = this.formEdit.eatBackMinRate
              this.tableData[i].eatBackMaxRate = this.formEdit.eatBackMaxRate
              break
            }
          }
        }
      })
    },
    onImport () {
      // 导入EXCEL
      importChangeStatuses(this.results).then((data) => {
        if (!data) {
          return
        }
        let success = data.data.data.intValues[0]
        if (success === 1) {
          this.$Message.info('导入excel成功!!!')
          this.onSearch()
        }
      })
    },
    onExport () {
      // 导出EXCEL
      getAllChangeStatuses().then((data) => {
        if (!data) {
          return
        }

        let allTableData = []
        let ChangeStatusVO = getVO('ChangeStatusVO')
        let pv = data.data.data
        let len = pv.data.length
        for (let i = 0; i < len; i++) {
          let vo = ChangeStatusVO.decode(pv.data[i])
          allTableData.push(vo)
        }
        console.log(allTableData)
        exportExcel(this.filename, allTableData)
      })
    },
    onFileChange (e) {
      var files = e.target.files || e.dataTransfer.files
      if (!files.length) {
        return
      }
      importExcel(e.target, this.setResults)
    },
    setResults (res) {
      this.results = res
      console.log('this.results:', this.results)
    }
  },
  computed: {
    tableColumns () {
      let cols = [
        {
          title: 'gameId',
          key: 'gameId',
          align: 'center'
        },
        {
          title: 'machineType',
          key: 'machineType',
          align: 'center'
        },
        {
          title: 'gameType',
          key: 'gameType',
          align: 'center'
        },
        {
          title: 'betId',
          key: 'betId',
          align: 'center'
        },
        {
          title: 'spitMinRate',
          key: 'spitMinRate',
          align: 'center'
        },
        {
          title: 'spitMaxRate',
          key: 'spitMaxRate',
          align: 'center'
        },
        {
          title: 'eatMinRate',
          key: 'eatMinRate',
          align: 'center'
        },
        {
          title: 'eatMaxRate',
          key: 'eatMaxRate',
          align: 'center'
        },
        {
          title: 'spitBackMinRate',
          key: 'spitBackMinRate',
          align: 'center'
        },
        {
          title: 'spitBackMaxRate',
          key: 'spitBackMaxRate',
          align: 'center'
        },
        {
          title: 'eatBackMinRate',
          key: 'eatBackMinRate',
          align: 'center'
        },
        {
          title: 'eatBackMaxRate',
          key: 'eatBackMaxRate',
          align: 'center'
        },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                on: {
                  click: () => {
                    let idx = params.index
                    this.showEditChangeStatus(idx)
                  }
                }
              }, '编辑')
            ])
          }
        }
      ]
      return cols
    }
  }
}

</script>

<style scoped>
.log-str{
  font-size: 14px;
  margin-bottom: 2px;
}
.content{
  min-height: 460px;
}
.bottom-page{
  margin-top: 5px;
  margin-bottom: 10px;
  float: right;
}

.bottom-table{
  margin-top: 5px;
  margin-bottom: 10px;
}
</style>
